package com.suning.sawp.service.impl.precharge;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suning.rsc.dto.responsedto.MbfServiceResponse;
import com.suning.rsc.mqservice.ei.annotation.EsbEIServerService;
import com.suning.rsc.server.NullResponseDto;
import com.suning.rsc.server.mq.MQServerServiceHandler;
import com.suning.sawp.constants.CacheKeyConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.NsfNullReponseInterfaceForEsb;
import com.suning.sawp.dto.precharge.PosActReturnBeanOld;
import com.suning.sawp.dto.precharge.PosRulesReturnBean;
import com.suning.sawp.intf.dao.PreChargeDaoService;
import com.suning.sawp.po.precharge.PrechargeActivity;
import com.suning.sawp.po.precharge.PrechargeRule;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.service.util.RedisCacheUtils;
/**
 * 
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 * 已废弃
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@EsbEIServerService(connectionFactory = "posConnectionFactoryAdapter", receiveQueue = "posResponseMDPQueue")
public class RecPrechargeActListenerOld extends MQServerServiceHandler<PosActReturnBeanOld, NullResponseDto, NsfNullReponseInterfaceForEsb>{

 private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_INTEGRATION);
    
    @Resource
    PreChargeDaoService preChargeDaoService;
    
    @Autowired
    RedisCacheUtils  cacheUtils;
    
    private static final String DATE_FORMAT = "yyyyMMdd";
    
    private static final String POS_MQ_PRO_FLAG_DEL = "1";
    
    private static final BigDecimal HUNDRED = new BigDecimal(100);
    
    @Override
    public NullResponseDto handleBizz(PosActReturnBeanOld actReturn, String uid) {
        LOGGER.info("店+接收预充值规则MQ消息Old:{},uid{}",actReturn.toString(),uid);
        try{
            if(POS_MQ_PRO_FLAG_DEL.equals(actReturn.getFlag())){
                preChargeDaoService.updateActStatusByRuleCode(actReturn.getRuleCode());
                cacheUtils.clearCacheData(CacheKeyConstants.PRECHARGE_ACT);
            }else{
                savePrechargeAct(actReturn);
                cacheUtils.clearCacheData(CacheKeyConstants.PRECHARGE_ACT, actReturn.getStoreCode());
            }
            cacheUtils.clearCacheData(CacheKeyConstants.UNEXPIRE_ACT_LIST);
        }catch(Exception e){
            LOGGER.error("店+处理预充值规则异常",e);
        }
        return null;
    }

    private void savePrechargeAct(PosActReturnBeanOld actReturn){
        if(StringUtils.isNotBlank(actReturn.getRuleCode()) && StringUtils.isNotBlank(actReturn.getStoreCode())){
            PrechargeActivity query = new PrechargeActivity();
            query.setStoreCode(actReturn.getStoreCode());
            query.setRuleCode(actReturn.getRuleCode());
            List<PrechargeActivity> list = preChargeDaoService.queryAct(query);
            if(CollectionUtils.isNotEmpty(list)){
                return;
            }
        }
        PrechargeActivity act = new PrechargeActivity();
        act.setStoreCode(actReturn.getStoreCode());
        act.setRuleCode(actReturn.getRuleCode());
        String effectiveDate = actReturn.getEffectiveDate();
        Integer effectiveDays = Integer.valueOf(actReturn.getEffectiveDays());
        Date date = DateUtils.getDateByStr(effectiveDate, DATE_FORMAT);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, effectiveDays - 1);
        act.setEffectiveDate(Integer.valueOf(effectiveDate));
        act.setEffectiveDays(effectiveDays);
        act.setInvalidDate(Integer.valueOf(DateUtils.getDateStrByFormat(ca.getTime(), DATE_FORMAT)));
        act.setPromoStartTime(Integer.valueOf(DateUtils.getDateStrByFormat(new Date(), DATE_FORMAT)));
        act.setPromoEndTime(Integer.valueOf(actReturn.getPromoDeadline()));
        act.setPromoStatus(PrechargeActivity.promoStatusType.ONSALE);
        act.setRuleType(actReturn.getRuleType());
        act.setMaxReturn(Integer.valueOf(actReturn.getMaxReturn()));
        int promotionId = preChargeDaoService.insertPrechargeAct(act);
        
        List<PosRulesReturnBean> rulesList = actReturn.getRulesList();
        if(CollectionUtils.isNotEmpty(rulesList)){
            for(PosRulesReturnBean rule : rulesList){
                PrechargeRule prechargeRule = new PrechargeRule();
                prechargeRule.setRuleBase(new BigDecimal(rule.getRuleBase()).multiply(HUNDRED).longValue());
                prechargeRule.setPromotionId(Long.valueOf(promotionId));
                prechargeRule.setCouponAmount(new BigDecimal(rule.getCouponAmount()).multiply(HUNDRED).longValue());
                prechargeRule.setMaxCouponAmount(new BigDecimal(rule.getMaxCouponAmount()).multiply(HUNDRED).longValue());
                preChargeDaoService.insertRule(prechargeRule);
            }
        }
    }
    
    @Override
    public void sendResponse(NsfNullReponseInterfaceForEsb clientInterface,
            NullResponseDto response, MbfServiceResponse serviceResponse) {
        // TODO Auto-generated method stub
        
    }

}
